import numpy as np

def convert_point(point,size=50):
    return (point[0] - size//2, point[1] - size//2)



def point_to_line_vector(ponit,ponit1, ponit2):
    """ 计算点ponit 到点（ponit1，ponit2）的垂线的方向向量 """
    
    j,k=ponit
    x,y=ponit1
    x1,y1=ponit2

    
    # 计算直线的方向向量
    d = np.array([x1 - x, y1 - y])
    
    # 计算直线的法向量
    n = np.array([-(y1 - y), x1 - x])
    
    # 归一化法向量
    n_normalized = n / np.linalg.norm(n)
    
    # 计算点到直线的垂线的交点
    # 直线的参数方程
    t = ((j - x) * (x1 - x) + (k - y) * (y1 - y)) / (d[0]**2 + d[1]**2)
    closest_point = np.array([x + t * d[0], y + t * d[1]])
    
    # 从点(j, k)到直线最近点的向量
    vector_to_line = closest_point - np.array([j, k])
    
    # 归一化向量
    normalized_vector = vector_to_line / np.linalg.norm(vector_to_line)
    
    return normalized_vector


# print(point_to_line_vector((0,0),(0,1),(2,1)))
def is_intersect(p1,s1, p2,s2,borderCover=False):
    """判断两个方块是否重叠"""
    p1_x, p1_y =convert_point(p1,50)
    p2_x, p2_y =convert_point(p2,50)
    # 计算物体的边界
    left1, right1 = p1_x,p1_x + s1
    top1, bottom1 = p1_y, p1_y+ s1
    
    left2, right2 = p2_x, p2_x + s2
    top2, bottom2 = p2_y, p2_y + s2
    
    
    if borderCover:
        # 检查是否有边重叠
        border=not (p1_x + s1 < p2_x or  # p1 在 p2 的左边
                p1_x > p2_x + s2 or  # p1 在 p2 的右边
                p1_y + s1 < p2_y or  # p1 在 p2 的上边
                p1_y > p2_y + s2)
        
        # 四个角是否相交
        top_left=right1==left2 and bottom1==top2
        top_right=left1==right2 and bottom1==top2
        bottom_left=right1==left2 and top1==bottom2
        bottom_right=left1==right2 and top1==bottom2
        
        corner=not (top_left or top_right or bottom_left or bottom_right)
        
        return border and corner
        
    else:
        return not (p1_x + s1 <= p2_x or  # p1 在 p2 的左边
                p1_x >= p2_x + s2 or  # p1 在 p2 的右边
                p1_y + s1 <= p2_y or  # p1 在 p2 的上边
                p1_y >= p2_y + s2)
 
def homePos(map):
    for i in range(map.shape[0]):
        for j in range(map.shape[1]):
            if map[i][j]=='FH':
                return (i*50+50,j*50+50)
            

def one_zero_map(map):
    new_map=np.zeros(map.shape)
    for i in range(map.shape[0]):
        for j in range(map.shape[1]):
            if map[i][j]=='G' or map[i][j]=="M":
                new_map[i][j]=1
    return new_map

def crd_game_to_gird(ponit):
    """游戏坐标转网格坐标"""
    x,y=ponit
    return (x//50,y//50)

def crd_grid_to_game(point,size=50):
    """网格坐标转游戏坐标"""
    x,y=point
    return (x*50+size//2,y*50+size//2)
def is_colliding(p1, size1, p2, size2):
    # 计算物体的边界
    left1, right1 = p1[0], p1[0] + size1
    top1, bottom1 = p1[1], p1[1] + size1
    
    left2, right2 = p2[0], p2[0] + size2
    top2, bottom2 = p2[1], p2[1] + size2
    
    # 检查是否有边重叠
    horizontal_overlap = (right1 >= left2 and left1 <= right2)
    vertical_overlap = (bottom1 >= top2 and top1 <= bottom2)
    
    # 只有一条边重合
    edge_touch = (right1 == left2 or left1 == right2) or (bottom1 == top2 or top1 == bottom2)
    
    # 判断是否碰撞
    return (horizontal_overlap and vertical_overlap) or edge_touch
# print(is_intersect((0,0),2,(2,2),2,True))
# print(one_zero_map(np.array([['G','F','F'],["F",'G','g']])))
# print(is_intersect((25, 25),50, (75, 75),50))
def larger_normalized_projection_signed(p1, p2):
    # 计算两个点在xy方向上较大的一个投影，并对投影归一化
    p1 = np.array(p1)
    p2 = np.array(p2)
    
    # Calculate the vector between the points
    vector = p2 - p1
    
    # Get the projections on the x and y axes
    proj_x = vector[0]  # x-axis projection (retain sign)
    proj_y = vector[1]  # y-axis projection (retain sign)
    
    # Determine the larger projection (in magnitude) and normalize it
    if abs(proj_x) > abs(proj_y):
        return tuple(np.array([np.sign(proj_x), 0]).tolist())  # Normalized x-direction projection
    else:
        return tuple(np.array([0, np.sign(proj_y)]).tolist())  # Normalized y-direction projection

def crd_2_3(point):
    x,y=crd_game_to_gird(point)
    nx,ny=x//8,y//10
    return (nx,ny)
    
def parse_map(stat):
  map=np.array(stat['map'])
  player=[p for p in stat['playerStateVos'] if p['playerId']==stat['playerId']][0]
  if 3 in player['holdingProps']:
    for i in range(0,map.shape[0]):
      for j in range(0,map.shape[1]):
        if map[i][j] in ['R','T',"W"]:
          map[i][j]='G'
  return map